import * as handel from '../app-configure/handle';
import * as setting from '../app-configure/setting';
import * as executor from './executor';
// @ts-ignore
// 忽略报错

enum EventType {
    /**AppInit准备完毕 */
    EVENT_APPINIT_FINISHED = 'EVENT_APPINIT_FINISHED',
    /**系统Manager初始化完毕 */
    EVENT_SYS_MANAGER_INITED = 'EVENT_SYS_MANAGER_INITED',
    /**用户Manager初始化完毕 */
    EVENT_USER_MANAGER_INITED = 'EVENT_USER_MANAGER_INITED',
    /**所有Manager初始化完毕 */
    EVENT_MANAGER_INITED = 'EVENT_MANAGER_INITED',
    /**所有Manager准备完毕 */
    EVENT_MANAGER_FINISHED = 'EVENT_MANAGER_FINISHED'
}

const EventMap = {};

export class App extends cc.EventTarget {
    appReady = false;
    appInited = false;
    cccReady = false;
    cccInited = false;

    static EventType = EventType;

    on(event: keyof typeof EventType, callback: Function, target?: any): any {
        if (EventMap[event]) callback.call(target);
        super.on(event, callback, target);
    }

    once(event: keyof typeof EventType, callback: Function, target?: any): any {
        if (EventMap[event]) {
            callback.call(target);
        } else {
            super.once(event, callback as any, target);
        }
    }

    emit(event: keyof typeof EventType, ...args: any[]): any {
        EventMap[event] = true;
        super.emit(event, ...args);
    }

    log(str: string, ...args: any[]) { console.log(`[app] [log] ${str}`, ...args); }
    warn(str: string, ...args: any[]) { console.warn(`[app] [warn] ${str}`, ...args); }
    error(str: string, ...args: any[]) { console.error(`[app] [error] ${str}`, ...args); }

    // 库
    lib = executor.lib;
    // 设置
    setting = setting;
    // 数据
    config = executor.config;
    data = executor.data;
    // manager
    Manager = executor.Manager;
    manager = executor.manager;
}

export const app: App = new App();

if (CC_DEV) {
    window['app'] = app;
    window['App'] = App;
}

if (!CC_EDITOR) {
    app.cccReady = true; app.appReady = true;
    handel.cccReady(); handel.appReady();
    cc.game.once(cc.game.EVENT_ENGINE_INITED, function () { app.cccInited = true; handel.cccInited(); });
    app.once(App.EventType.EVENT_APPINIT_FINISHED, function () { app.appInited = true; handel.appInited(); });
}